#include "mainsystemwidget.h"
#include "ui_mainsystemwidget.h"
#include <QDebug>
#include <QMessageBox>

MainSystemWidget::MainSystemWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainSystemWidget),
    m_pDatabaseManager(nullptr),
    m_pUserManager(UserManager::instance()),
    m_pStackedLayout(nullptr),
    m_pAdminManagement(nullptr),
    m_pCommonUserManagement(nullptr),
    m_pDeviceManagement(nullptr),
    m_pDataMonitoring(nullptr),
    m_pAlertManagement(nullptr),
    m_pDataAnalysis(nullptr),
    m_pSystemSetting(nullptr),
    m_pLogManagement(nullptr),
    m_pUart(nullptr)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    initialize();

    QString role = m_pUserManager->getCurrentUserRole();
    if(role == "admin") {
        m_pStackedLayout->setCurrentWidget(m_pAdminManagement);
    } else {
        m_pStackedLayout->setCurrentWidget(m_pCommonUserManagement);
    }
}

void MainSystemWidget::initialize()
{
    // 初始化数据库管理器
    m_pDatabaseManager = DatabaseManager::instance();
    if (!m_pDatabaseManager) {
        QMessageBox::critical(this, "错误", "无法初始化数据库管理器");
        return;
    }

    // 初始化用户管理器
    if (m_pUserManager) {
        m_pUserManager->setDatabaseManager(m_pDatabaseManager);
    } else {
        QMessageBox::warning(this, "警告", "用户管理器未初始化");
    }

    // 初始化界面组件
    initUIComponents();
    setupConnections();
}

void MainSystemWidget::initUIComponents()
{
    // 创建堆栈布局
    m_pStackedLayout = new QStackedLayout(ui->mainSystemLayout);

    // 初始化各功能页面
    m_pAdminManagement = new AdminManagement(this);
    m_pCommonUserManagement = new CommonUserManagement(this);
    m_pDeviceManagement = new DeviceManagement(this);
    m_pDataMonitoring = new DataMonitoring(this);
    m_pAlertManagement = new AlertManagement(this);
    m_pDataAnalysis = new DataAnalysis(this);
    m_pSystemSetting = new SystemSetting(this);
    m_pLogManagement = new LogManagement(this);
    m_pUart = new Uart(this);

    // 设置管理器
    if (m_pUserManager) {
        m_pAdminManagement->setUserManager(m_pUserManager);
        m_pCommonUserManagement->setUserManager(m_pUserManager);
    }
    if (m_pDatabaseManager) {
        m_pDeviceManagement->setDatabaseManager(m_pDatabaseManager);
    }

    // 添加页面到布局
    m_pStackedLayout->addWidget(m_pAdminManagement);
    m_pStackedLayout->addWidget(m_pCommonUserManagement);
    m_pStackedLayout->addWidget(m_pDeviceManagement);
    m_pStackedLayout->addWidget(m_pDataMonitoring);
    m_pStackedLayout->addWidget(m_pAlertManagement);
    m_pStackedLayout->addWidget(m_pDataAnalysis);
    m_pStackedLayout->addWidget(m_pSystemSetting);
    m_pStackedLayout->addWidget(m_pLogManagement);
    m_pStackedLayout->addWidget(m_pUart);
}

void MainSystemWidget::setupConnections()
{
    connect(ui->btnUserManagement, &QPushButton::clicked, this, &MainSystemWidget::onUserManagementClicked);
    connect(ui->btnDeviceManagement, &QPushButton::clicked, this, &MainSystemWidget::onDeviceManagementClicked);
    connect(ui->btnDataMonitoring, &QPushButton::clicked, this, &MainSystemWidget::onDataMonitoringClicked);
    connect(ui->btnAlertManagement, &QPushButton::clicked, this, &MainSystemWidget::onAlertManagementClicked);
    connect(ui->btnDataAnalysis, &QPushButton::clicked, this, &MainSystemWidget::onDataAnalysisClicked);
    connect(ui->btnSystemSettings, &QPushButton::clicked, this, &MainSystemWidget::onSystemSettingClicked);
    connect(ui->btnLogManagement, &QPushButton::clicked, this, &MainSystemWidget::onLogManagementClicked);
    connect(ui->btnUart, &QPushButton::clicked, this, &MainSystemWidget::onUartMangementClicked);

}

void MainSystemWidget::onUserManagementClicked()
{
    if (!m_pUserManager || !m_pStackedLayout) {
        qWarning() << "UserManager or StackedLayout not initialized";
        return;
    }

    QString currentRole = m_pUserManager->getCurrentUserRole();
    qDebug() << "切换到用户管理页面，当前角色:" << currentRole;

    if (currentRole == "admin" && m_pAdminManagement) {
        m_pStackedLayout->setCurrentWidget(m_pAdminManagement);
        m_pAdminManagement->refreshTable();
    } else if (m_pCommonUserManagement) {
        m_pStackedLayout->setCurrentWidget(m_pCommonUserManagement);
        m_pCommonUserManagement->refreshTable();
    }
}

void MainSystemWidget::onDeviceManagementClicked()
{
    if (m_pStackedLayout && m_pDeviceManagement) {
        m_pStackedLayout->setCurrentWidget(m_pDeviceManagement);
        m_pDeviceManagement->refreshDeviceList();
    }
}

void MainSystemWidget::onDataMonitoringClicked()
{
    if (m_pStackedLayout && m_pDataMonitoring) {
        m_pStackedLayout->setCurrentWidget(m_pDataMonitoring);
        m_pDataMonitoring->startDataRefresh();
    }
}

void MainSystemWidget::onAlertManagementClicked()
{
    if (m_pStackedLayout && m_pAlertManagement) {
        m_pStackedLayout->setCurrentWidget(m_pAlertManagement);
    }
}

void MainSystemWidget::onDataAnalysisClicked()
{
    if (m_pStackedLayout && m_pDataAnalysis) {
        m_pStackedLayout->setCurrentWidget(m_pDataAnalysis);
        m_pDataAnalysis->loadDeviceList();
        m_pDataAnalysis->updateCharts();
    }
}

void MainSystemWidget::onSystemSettingClicked()
{
    if (m_pStackedLayout && m_pSystemSetting) {
        m_pStackedLayout->setCurrentWidget(m_pSystemSetting);
    }
}

void MainSystemWidget::onLogManagementClicked()
{
    if (m_pStackedLayout && m_pLogManagement) {
        m_pStackedLayout->setCurrentWidget(m_pLogManagement);
    }
}

void MainSystemWidget::onUartMangementClicked(){
    m_pStackedLayout->setCurrentWidget(m_pUart);
}

MainSystemWidget::~MainSystemWidget()
{
    // 清理资源
    if (m_pAdminManagement) delete m_pAdminManagement;
    if (m_pCommonUserManagement) delete m_pCommonUserManagement;
    if (m_pDeviceManagement) delete m_pDeviceManagement;
    if (m_pDataMonitoring) delete m_pDataMonitoring;
    if (m_pAlertManagement) delete m_pAlertManagement;
    if (m_pDataAnalysis) delete m_pDataAnalysis;
    if (m_pSystemSetting) delete m_pSystemSetting;
    if (m_pLogManagement) delete m_pLogManagement;
    if (m_pStackedLayout) delete m_pStackedLayout;
    delete ui;
}

void MainSystemWidget::switchToRole(int role) {
    if(role == 0) { // 假设0表示管理员
        m_pStackedLayout->setCurrentWidget(m_pAdminManagement);
    } else {
        m_pStackedLayout->setCurrentWidget(m_pCommonUserManagement);
    }
}

